package com.dmall.chapter05.section04;

import com.dmall.chapter02.section09.Graph;
import com.dmall.chapter05.section03.SingleSourcePath;

/**
 * 所有点的路径问题
 * 基于图的广度优先遍历
 * @author wx
 * @date 2020-02-27 21:26:11
 */
public class AllPairsPath {

    private Graph G;
    private SingleSourcePath[] paths;

    public AllPairsPath(Graph G) {
        this.G = G;
        paths = new SingleSourcePath[G.V()];
        for (int v = 0; v < G.V(); v++) {
            paths[v] = new SingleSourcePath(G, v);
        }
    }

    public boolean isConnectedTo(int s, int t) {
        G.validateVertex(s);
        return paths[s].isConnectedTo(t);
    }

    public Iterable<Integer> path(int s, int t) {
        G.validateVertex(s);
        return paths[s].path(t);
    }

    public static void main(String[] args) {
        String filename = AllPairsPath.class.getClassLoader().getResource("g.txt").getPath();
        Graph g = new Graph(filename);
        AllPairsPath singleSourcePath = new AllPairsPath(g);

        System.out.println("0 -> 6 : " + singleSourcePath.path(0, 6));

        System.out.println("0 -> 5 : " + singleSourcePath.path(0, 5));
    }
}
